package com.rwhansen.humidor;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

public class MyInfo {
	
	public static Database humidorDatabase;


	JFrame frmStats;
	private JTextField txtMyName;
	private JTextField txtHumidor;
	private String humidorDatabaseFileName;

	public MyInfo() {
		frmStats = new JFrame();
		frmStats.setIconImage(Toolkit.getDefaultToolkit().getImage(MyInfo.class.getResource("/com/rwhansen/humidor/favicon.ico")));
		frmStats.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		frmStats.setTitle("My Information");
		frmStats.setAlwaysOnTop(true);
		frmStats.setBounds(100, 100, 510, 136);
		frmStats.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		humidorDatabaseFileName = Database.HUMIDOR_DB_FILENAME;
		humidorDatabase = new Database(humidorDatabaseFileName);
		File file2 = new File(humidorDatabaseFileName);
		boolean exists2 = file2.exists();
		if (exists2) {
			humidorDatabase = Database.readDatabase(humidorDatabaseFileName);
		}
		
		JPanel panel = new JPanel();
		frmStats.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblName = new JLabel("Name");
		lblName.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		lblName.setBounds(10, 12, 111, 14);
		panel.add(lblName);
		
		JLabel lblHumidorName = new JLabel("Humidor Name");
		lblHumidorName.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		lblHumidorName.setBounds(10, 41, 111, 14);
		panel.add(lblHumidorName);
		
		txtMyName = new JTextField();
		txtMyName.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		txtMyName.setText(humidorDatabase.getOwner());
		txtMyName.setBounds(118, 9, 366, 20);
		panel.add(txtMyName);
		txtMyName.setColumns(10);
		
		txtHumidor = new JTextField();
		txtHumidor.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		txtHumidor.setText(humidorDatabase.getName());
		txtHumidor.setBounds(118, 38, 366, 20);
		panel.add(txtHumidor);
		txtHumidor.setColumns(10);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//TODO: find a way to save name and humidor
				
				humidorDatabase.setOwner(txtMyName.getText());
				humidorDatabase.setName(txtHumidor.getText());
				
				Database.writeDatabase(humidorDatabaseFileName, humidorDatabase);
				frmStats.setVisible(false);
				
			}
		});
		btnSave.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		btnSave.setBounds(257, 67, 213, 23);
		panel.add(btnSave);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frmStats.setVisible(false);
			}
		});
		btnCancel.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		btnCancel.setBounds(22, 67, 213, 23);
		panel.add(btnCancel);
		
	}
}
